﻿using System;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using DataDynamics.ActiveReports;
using System.IO;
using System.Text;

 
    /// <summary>
    /// BillReport 的摘要说明。

    /// </summary>
    public partial class BillReport : System.Web.UI.Page
    {
        //protected System.Web.UI.HtmlControls.HtmlInputHidden hdReportModule;
        //protected System.Web.UI.HtmlControls.HtmlInputHidden hdReportName;
        //protected System.Web.UI.HtmlControls.HtmlInputHidden hdBillDJBH;
        //protected System.Web.UI.HtmlControls.HtmlInputHidden hdPrintType;
        //protected System.Web.UI.HtmlControls.HtmlInputHidden hdJeControl;

        private void Page_Load(object sender, System.EventArgs e)
        {

            //string sReturnReport = this.Page.Request.QueryString["ReturnReport"]; 
            //string sReportModule = this.Page.Request.QueryString["ReportModule"];
            //sReportModule = sReportModule.Replace("/", "\\");
            //string sReportName = this.Page.Request.QueryString["ReportName"];
            //string sDJBH = this.Page.Request.QueryString["DJBH"];
            //string sPrintType = this.Page.Request.QueryString["PrintType"];
            //string sJeControl = this.Page.Request.QueryString["JECONTROL"];

            string sReturnReport =  this.Page.Request.QueryString["ReturnReport"];//"1";//
            string sReportModule = this.Page.Request.QueryString["ReportModule"];// "phgl/SdPhd"; 
            sReportModule = sReportModule.Replace("/", "\\");
            string sReportName =  this.Page.Request.QueryString["ReportName"]; //"Report22";"qtlsd";//
            string sDJBH =  this.Page.Request.QueryString["DJBH"];// "XP0604200001";
            string sPrinter = this.Page.Request.QueryString["PRINTER"];
            string sPrintType = this.Page.Request.QueryString["PrintType"];  //"XP0604200001";//"0"; //
            string sJeControl = "0";// this.Page.Request.QueryString["JECONTROL"]; 

            if (sJeControl == null) sJeControl = "0";
            this.hdBillDJBH.Value = sDJBH;
            this.hdPrinter.Value = sPrinter;
            this.hdReportModule.Value = sReportModule;
            this.hdReportName.Value = sReportName;
            this.hdJeControl.Value = sJeControl;
            if (sPrintType != null) this.hdPrintType.Value = sPrintType.Trim();

            if ((sReturnReport != null) && (sReturnReport.Trim().Length != 0)) //DBUtility.DBHelper.connString;  // data source=192.168.176.29;initial catalog=bstyle_erp;persist security info=True;user id=sa 
            {
                //报表初始化

                string sDBConnStr = DBUtility.DBHelper.connString;//  
                string sReportPath =ConfigurationManager.AppSettings["ReportPath"];

                //for (int i = 0; i < sDJBH.Split('|').Length; i++)
                //{
                    //string sDBConnStr = ConfigurationSettings.AppSettings["connectionStr"];
                    this.Page.Response.Buffer = true;
                    ActiveReport3 rpt = null;
                    rpt = new ActiveReport3();
                    string sPath = sReportPath + sReportModule + @"\";
                    MemoryStream outStream = new MemoryStream();
                    byte[] bytes = null;
                    try
                    {
                        if (File.Exists(sPath + sReportName + ".rpx"))
                        {
                            rpt.LoadLayout(sPath + sReportName + ".rpx");
                            ((DataDynamics.ActiveReports.DataSources.SqlDBDataSource)rpt.DataSource).ConnectionString = sDBConnStr;
                            rpt.UserData = sPath;/// +sReportName + ".rpx"; 
                            rpt.Parameters["DJBH"].Value = sDJBH;//.Split('|')[i];
                            rpt.Parameters["PRINTER"].Value = sPrinter;
                            rpt.ShowParameterUI = false;
                            rpt.Run();
                        }
                        //for (int i = 0; i < rpt.Sections.Count; i++) rpt.Sections[i].Tag = sJeControl; 
                        //定义客户自定义的文件
                    }
                    catch (DataDynamics.ActiveReports.ReportException eRunReport)
                    {
                        this.Trace.Warn("Report failed to run:\n" + eRunReport.ToString());
                    }
                    // Create a memory stream to put the report document RDF in

                    // Save the report document into the memory stream
                    rpt.Document.Save(outStream, DataDynamics.ActiveReports.Document.RdfFormat.AR20);

                    // Move the postion back to the beginning of the stream
                    outStream.Seek(0, SeekOrigin.Begin);

                    // Create a byte array buffer to read the memory stream into
                    bytes = new byte[outStream.Length];
                    // Fill the byte array buffer with the bytes from the memory stream
                    outStream.Read(bytes, 0, (int)outStream.Length);
                    this.Page.Response.ContentEncoding = Encoding.UTF8;
                    // Clear anything that might have been written by the aspx page
                    this.Page.Response.ClearContent();
                    this.Page.Response.ClearHeaders();
                    // Write the report document byte array to the requestor:
                    this.Page.Response.BinaryWrite(bytes);

                      // end this request/response  
                //}
                this.Page.Response.End();
            }
        }


        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion


    } 

